package cn.liyicun.school.car2.interceptor;

import java.io.PrintWriter;
import java.io.StringWriter;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

import cn.liyicun.school.car2.base.ResultBean;
import cn.liyicun.school.car2.error.ErrorRunTimeException;
import cn.liyicun.school.car2.error.LoginRunTimeException;

public class ErrorActionInterceptor implements Interceptor {

	public void intercept(Invocation inv) {
		System.out.println("------------------ErrorActionInterceptor----------");
		try {
			inv.getController().setAttr("_now", System.nanoTime());
			inv.invoke();
		} catch (LoginRunTimeException e) {
			Controller con = inv.getController();
			con.renderJson(new ResultBean(601, "未登陆"));
			return;
		} catch (ErrorRunTimeException e) {
			Controller con = inv.getController();
			con.renderJson(new ResultBean(e.getCode(), e.getMessage()));
			return;
		} catch (Exception e) {
			Controller con = inv.getController();
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			con.renderJson(new ResultBean(500, e.getMessage(), sw.toString()));
			return;
		}

	}

}
